class EventsController < ApplicationController
  
  def map
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @locations }
    end
  end
  
  def find
	location=Event.all#selecciono todas las locaciones (se puede hacer un query seleccionando solo las que sirven)
	#latmin=params[:latmin]
	#latmax=params[:latmax]
	#lngmin=params[:lngmin]
	#lngmax=params[:lngmax]
	latmin=12
	latmax=15
	lngmin=-100
	lngmax=100
	locations_rendered= Array.new
	location.each do |t|#recorro las locaciones para quedarme solo con las que me sirven 
		if t.latitude !=nil || t.longitude != nil
			if ((t.latitude>latmin && latmax>t.latitude) && (t.longitude>lngmin && lngmax>t.longitude))
				locations_rendered << t #las meto en un array
			end
		end
	end	
	#@usuario=User.find(:first, :conditions => { :id => 1})
	respond_to do |format|
      #render :text => @location.lat	
	  format.json  { render :json => locations_rendered } #lo rendereo como json
    end
 end
  # GET /locations/add
  # GET /locations/add.xml
 def add
	@location=Location.new
	@location.latitude=params[:lat]
	@location.longitude=params[:lng]
	@location.save#guardo las coordenadas que me mandan como parametros
	#@usuario=User.find(:first, :conditions => { :id => 1})
	#respond_to do |format|
      render :text => @location.latitude #se puede no incluir
	  #format.json  { render :json => @location }
    #end
 end
  # GET /events
  # GET /events.xml
  def index
    @events = Event.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @events }
    end
  end

  # GET /events/1
  # GET /events/1.xml
  def show
    @event = Event.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @event }
    end
  end

  # GET /events/new
  # GET /events/new.xml
  def new
    @event = Event.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @event }
    end
  end

  # GET /events/1/edit
  def edit
    @event = Event.find(params[:id])
  end

  # POST /events
  # POST /events.xml
  

  def create
    @event = Event.new(params[:event])
	tagid = params[:Categoria]
		
    respond_to do |format|
      if @event.save
		t = EventTag.new
		t.events_id = @event.id
		t.tags_id =tagid
		t.save
	
        flash[:notice] = 'Event was successfully created.'
        format.html { redirect_to(@event) }
        format.xml  { render :xml => @event, :status => :created, :location => @event }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @event.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /events/1
  # PUT /events/1.xml
  def update
    @event = Event.find(params[:id])
		
	
    respond_to do |format|
      if @event.update_attributes(params[:event])
	
        flash[:notice] = 'Event was successfully updated.'
        format.html { redirect_to(@event) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @event.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /events/1
  # DELETE /events/1.xml
  def destroy
    @event = Event.find(params[:id])
    @event.destroy

    respond_to do |format|
      format.html { redirect_to(events_url) }
      format.xml  { head :ok }
    end
  end
  
  
  def iliked
flash[:notice] = 'You liked.'
@event = Event.find(params[:id])
	@event.good_rating = @event.good_rating + 1
	ar = @event.good_rating 
	b = @event.bad_rating
	@event.rating =  (ar*10)/(ar+b)
		a =current_user.id
		users_id = params[:users_id]
		events_id = params[:events_id]
		rat = Rated.count(:all,:conditions => ["users_id=? and events_id=?",a,@event.id])
		
		if( rat == 0)
			@event.rated = 1
			r = Rated.new
			r.users_id = a
			r.events_id = @event.id
			r.save
			if @event.update_attributes(params[:event])
				flash[:notice] = 'You liked.'
			else
				flash[:notice] = 'Error.'
			end
		else	
			flash[:notice] = 'You vote before'
		end	
   respond_to do |format|
      format.html { redirect_to(events_url) }
      format.xml  { head :ok }
    end
  
  end
  
   def idontliked
flash[:notice] = 'You dont liked.'
@event = Event.find(params[:id])
	@event.bad_rating = @event.bad_rating + 1
	ar = @event.good_rating 
	b = @event.bad_rating
	@event.rating =  (ar*10)/(ar+b)
		a =current_user.id
		users_id = params[:users_id]
		events_id = params[:events_id]
		rat = Rated.count(:all,:conditions => ["users_id=? and events_id=?",a,@event.id])
		if( rat == 0)
			r = Rated.new
			r.users_id = a
			r.events_id = @event.id
			r.save
			if @event.update_attributes(params[:event])
				flash[:notice] = 'You dont liked.'
			else
				flash[:notice] = 'Error.'
			end
		else	
			flash[:notice] = 'You vote before'
		end	
	
   respond_to do |format|
      format.html { redirect_to(events_url) }
      format.xml  { head :ok }
    end
  
  end
  
  
  
end
